import time
import pyautogui

import common
import config

import pyperclip

import random


def go():
    timeInterval = config.read().getint('Moment Set', 'timeMomentInterval')

    btn_moment = common.getPosCenter(common.getImgPath("btn_moment.png"))
    if btn_moment:
        common.moveToClick(btn_moment)
    else:
        return "没有发现朋友圈按钮"
    
    time.sleep(1)
    moment_window = common.moment_init()
    if moment_window:
        # print(moment_window)
        moment_window_left = moment_window.left
        moment_window_top = moment_window.top
        if (moment_window_left >= 5 and moment_window_top >= 5):
            pyautogui.moveTo(moment_window_left + 200, moment_window_top + 20)
            pyautogui.doubleClick()
            pyautogui.moveTo(400, 600)


        momentNumber = config.read().getint('Moment Set', 'momentNumber')
        momentAllNumber = 0
        while True:
            if momentAllNumber >= momentNumber:
                print("任务完成")
                break
            
            btn_moment_dots = pyautogui.locateAllOnScreen(
                common.getImgPath("btn_moment_dot.png"), 
                grayscale=True, 
                confidence=0.9)
            moment_dots = list(btn_moment_dots)
            # print(moment_dots)
            # reversed_moment_dots = reversed(moment_dots) # 先点后面的, 再点前面的
            for moment_dot in moment_dots:
                # print(moment_dot)
                common.moveToClick(pyautogui.center(moment_dot))
                time.sleep(0.5)
                btn_moment_like = common.getPosCenter(common.getImgPath("btn_moment_like.png"))
                if btn_moment_like:
                    momentAllNumber = momentAllNumber + 1

                    common.moveToClick(btn_moment_like)
                    time.sleep(0.1)

                    # 点赞+评论, 注意这个名字没有改, 这里是选择了点选加评论的选项
                    radioMomentLastMessage = config.read().getint('Moment Set', 'radioMomentLastMessage')
                    if radioMomentLastMessage == 0: break # 如果是点赞, 点击一次就返回

                    if radioMomentLastMessage == 1:
                        common.moveToClick(moment_dot)
                        time.sleep(0.1)

                        btn_moment_comment = common.getPosCenter(common.getImgPath("btn_moment_comment.png"))
                        if btn_moment_comment:
                            common.moveToClick(btn_moment_comment)
                            time.sleep(0.1)

                            content = config.read().get('Content Set', 'Content' + str(random.randint(1, 4)))

                            pyperclip.copy(content)
                            pyautogui.hotkey('ctrl', 'v')
                            time.sleep(0.1)
                            
                            pyautogui.hotkey('enter')
                            time.sleep(1)
                            break  # 每次只评论一次

            
            i = 0
            while True:
                pyautogui.scroll(-200)
                i = i + 1
                if i > 3:
                    time.sleep(1)
                    break


            # 发送延时
            if timeInterval == 0:
                time.sleep(random.randint(0, 1))
            elif timeInterval == 1:
                time.sleep(random.randint(1, 3))
            elif timeInterval == 2:
                time.sleep(random.randint(2, 6))
            elif timeInterval == 3:
                time.sleep(random.randint(4, 10))
            elif timeInterval == 4:
                time.sleep(random.randint(8, 20))                  
